package com.cpi.cornpp.biweeks.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.biweek.exception.BiWeekException;
import com.cpi.cornpp.biweeks.converter.BiWeekPeriodVersionConverter;
import com.cpi.cornpp.biweeks.dto.BiWeekPeriodVersionDTO;
import com.cpi.cornpp.biweeks.view.BiWeekPeriodVersionDAO;
import com.cpi.cornpp.biweeks.view.BiWeekPeriodVersionView;
import com.cpi.cornpp.biweeks.view.BiWeekView;

public class BiWeekPeriodVersionService {
	public List getBiWeekPeriodVersionsByVersion(Integer versionId) throws BiWeekException{
		BiWeekView biWeekView = null;
		BiWeekPeriodVersionView biWeekPeriodVersionView = null;
		BiWeekPeriodVersionDAO biWeekPeriodVersionDAO = null;
		BiWeekService biWeekService = null;
		List biWeekPeriodVersionsDTO = null;
		List biWeekPeriodVersionsView = null;
		
		biWeekPeriodVersionsView = new ArrayList(0);
		
		try{
			
			if (versionId != null){
				biWeekPeriodVersionDAO = new BiWeekPeriodVersionDAO();
				
				biWeekPeriodVersionsDTO = biWeekPeriodVersionDAO.getBiWeekPeriodVersionsByVersionId(versionId);
				
				if (CollectionUtils.isNotEmpty(biWeekPeriodVersionsDTO)){
					biWeekService = new BiWeekService();
					biWeekPeriodVersionsView = BiWeekPeriodVersionConverter.convertDTOList(biWeekPeriodVersionsDTO);
					
					for (Iterator itr = biWeekPeriodVersionsView.iterator(); itr.hasNext(); ){
						biWeekPeriodVersionView = (BiWeekPeriodVersionView) itr.next();
						biWeekView = biWeekService.getBiWeek(biWeekPeriodVersionView.getBiWeekPeriodId());
						
						biWeekPeriodVersionView.setBiWeekView(biWeekView);
					}
				}
			}
			
		} catch(BiWeekException bwe){
			throw bwe;
		} catch(Exception e){
			BiWeekException bwe = new BiWeekException(e, BiWeekException.LAYER_SERVICE, BiWeekException.ACTION_SELECT);
			
			e.printStackTrace(System.out);
			
			throw bwe;
		}
		
		return biWeekPeriodVersionsView;
	}
	public BiWeekPeriodVersionView getBiWeekPeriodVersion(Integer biWeekPeriodVersionId) throws BiWeekException{
		BiWeekPeriodVersionDTO biWeekPeriodVersionDTO = null;
		BiWeekPeriodVersionView biWeekPeriodVersionView = null;
		BiWeekPeriodVersionDAO biWeekPeriodVersionDAO = null;
		BiWeekService biWeekService = null;
		
		try{
			
			if (biWeekPeriodVersionId != null){
				biWeekPeriodVersionDAO = new BiWeekPeriodVersionDAO();
				
				biWeekPeriodVersionDTO = biWeekPeriodVersionDAO.getBiWeekVersionById(biWeekPeriodVersionId);
				
				if (biWeekPeriodVersionDTO != null){
					biWeekService = new BiWeekService();
					
					biWeekPeriodVersionView = BiWeekPeriodVersionConverter.convert(biWeekPeriodVersionDTO);
					
					biWeekPeriodVersionView.setBiWeekView(biWeekService.getBiWeek(biWeekPeriodVersionView.getBiWeekPeriodId()));
				}
			}
			
		} catch(BiWeekException bwe){
			throw bwe;
		} catch(Exception e){
			BiWeekException bwe = new BiWeekException(e, BiWeekException.LAYER_SERVICE, BiWeekException.ACTION_SELECT);
			
			e.printStackTrace(System.out);
			
			throw bwe;
		}
		
		return biWeekPeriodVersionView;
	}
}